//my.js
const app = getApp()
Page({
  data: {
    userData: [],
    showWithDrawViewal: false,
    signUp: null,
    homeData: null,
    withDrawViewalTotal: null,
    withDrawViewalTips: null,
    withDrawViewalSubmit: false,
    withDrawViewalInput: null,
    loadingHidden: false,
  },
  onShow: function () {
    var that = this;
    var intervalId = setInterval(function () {
      if (that.data.homeData == null || app.globalData.myDataHaveChange) {
        if (app.globalData.homeData != null && app.globalData.appGetDataFinish) {
          that.setData({
            userData: app.globalData.user,
            homeData: app.globalData.homeData,
            signUp: app.globalData.homeData.signUp
          });
          app.globalData.myDataHaveChange = false;
        }
      } else {
        that.setData({
          loadingHidden: true
        });
        clearInterval(intervalId);
      }
    }, 300);
  },
  /**
  *获取提现金额，并处理数据
  */
  getAmount: function (e) {
    var that = this;
    that.data.withDrawViewalInput = e;
    var inputValue = e.detail.value - 0;
    that.setData({
      withDrawViewalTotal: e.detail.value
    })
    if (inputValue < 2) {
      that.setData({
        withDrawViewalSubmit: false,
        withDrawViewalTips: '单次提现金额最少2元人民币。',
      });
    } else if (inputValue > 200) {
      that.setData({
        withDrawViewalSubmit: false,
        withDrawViewalTips: '单次提现金额最多200元人民币。',
      });
    } else if (inputValue * 0.07 < 2) {
      // 手续费小于2元
      var poundage = 2;
      var total = inputValue + poundage;
      if (inputValue * 100 + 200 > that.data.userData.balance) {
        that.setData({
          withDrawViewalSubmit: false,
          withDrawViewalTips: '账户余额不足。',
        });
      } else {
        that.setData({
          withDrawViewalSubmit: true,
          withDrawViewalTips: '手续费：' + poundage + '元；共计：' + total + '元',
        });
      }
    } else {
      // 手续费大于2元
      var poundage = inputValue * 0.07;
      var total = inputValue + poundage;
      if (inputValue * 107 > that.data.userData.balance) {
        that.setData({
          withDrawViewalSubmit: false,
          withDrawViewalTips: '账户余额不足。',
        });
      } else {
        that.setData({
          withDrawViewalSubmit: true,
          withDrawViewalTips: '手续费：' + poundage + '元；共计：' + total + '元',
        });
      }
    }
  },
  /**
   * 提现申请
   */
  withdrawalCash: function () {
    var that = this
    if (that.data.withDrawViewalSubmit) {
      wx.request({
        url: app.globalData.API_URL + 'user/withdrawals',
        data: {
          'token': that.data.userData.token,
          'price': (that.data.withDrawViewalTotal - 0) * 100,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'post',
        success: function (r) {
          if (r.data.code == 0) {
            wx.showToast({
              title: '提现申请提交成功！',
            })
            that.setData({
              userData: r.data.data,
            })
            app.globalData.user = r.data.data;
          } else {
            wx.showToast({
              title: r.data.msg,
            })
          }
        }
      })
    }
    that.setData({
      showWithDrawViewal: false,
      withDrawViewalInput: ''
    })
  },
  /**
   * 【进行中的】点击事件
   */
  TapInSignUp: function (e) {
    wx.navigateTo({
      url: '../inSignUp/inSignUp'
    })
  },
  /**
   * 【打卡记录】点击事件
   */
  TapPunchRecord: function (e) {
    wx.navigateTo({
      url: '../punchRecord/punchRecord'
    })
  },
  /**
   * 【我的账户】点击事件
   */
  TapWallet: function (e) {
    var that = this;
    that.setData({
      showWithDrawViewal: (!that.data.showWithDrawViewal),
      withDrawViewalTotal: "",
      withDrawViewalTips: null,
    })
  },
  /**
   * TODO: 【玩法规则】点击事件
   */
  TapRule: function (e) {
    wx.navigateTo({
      url: '../playRules/playRules'
    })
  },
  /**
   * 【问题咨询】点击事件
   */
  TapContact: function (e) {
    wx.navigateTo({
      url: '../ask/ask'
    })
  },
  /**
   * TODO: 【已结束任务】点击事件
   */
  TapEndSignUp: function (e) {
    wx.navigateTo({
      url: '../endSignUp/endSignUp'
    })
  }
})
